#include <algorithm>
#include <iostream>
#include <string>
#include <vector>


static inline constexpr int popcount(int x) {
    return __builtin_popcount(x);
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int n, m;
    std::cin >> n >> m;

    std::vector<int> map(n + 1, 0);
    for (int i = 1; i <= n; ++i) {
        std::string row;
        std::cin >> row;
        for (int j = 0; j < m; ++j) {
            if (row[j] == 'H') {
                map[i] |= (1 << j);
            }
        }
    }

    std::vector<int> states;
    std::vector<int> counts;
    for (int i = 0; i < (1 << m); ++i) {

        if (!((i << 1) & i) && !((i << 2) & i)) {
            states.push_back(i);
            counts.push_back(popcount(i));
        }
    }
    int num = states.size();

    std::vector<std::vector<std::vector<int>>> dp(n + 1, std::vector<std::vector<int>>(num, std::vector<int>(num, 0)));

    for (int i = 1; i <= n; ++i) {

        for (int j = 0; j < num; ++j) {
            int si = states[j];

            if (si & map[i]) {
                continue;
            }

            for (int k = 0; k < num; ++k) {
                int sim1 = states[k];

                if (si & sim1) {
                    continue;
                }

                for (int l = 0; l < num; ++l) {
                    int sim2 = states[l];

                    if ((si & sim2) || (sim1 & sim2)) {
                        continue;
                    }

                    dp[i][j][k] = std::max(dp[i][j][k], dp[i - 1][k][l] + counts[j]);
                }
            }
        }
    }

    int ans = 0;
    for (int j = 0; j < num; ++j) {
        for (int k = 0; k < num; ++k) {
            ans = std::max(ans, dp[n][j][k]);
        }
    }

    std::cout << ans << std::endl;

    return 0;
}
